package Arrays;

public class _565_ArrayNesting {
    public int arrayNesting(int[] nums) {
        boolean[] visited = new boolean[nums.length];
        int max = 0;
        for(int i=0;i<nums.length;i++) {
            int ptr = i;
            int count = 0;
            while (!visited[ptr]) {
                visited[ptr] = true;
                ptr = nums[ptr];
                count++;
            }
            if (count > max) {
                max = count;
            }
        }
        return max;
    }
}
